﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using Microsoft.SqlServer.Server;
using SD.Database.Classes;
using DBAccess;
using System.Collections.Generic;

namespace SD.Database
{
    public partial class Planets
    {
        public static void AddProductionPoints(int sid, int pid, int pp)
        {
            DAL.GetInstance().UpdateData("Update Planets SET ProductionPoints = ProductionPoints + @param0 Where SystemID = @param1 and PlanetID = @param2", new List<object>() { pp, sid, pid });
        }

        public static void ColonizePlanet(int sId, int pId, int owner)
        {
            try
            {
                DAL.GetInstance().UpdateData("Update Planets SET OwnerId= @param0, PopulationLevel =1, PopToNextLevel = 21 Where SystemID = @param1 and PlanetID = @param2", new List<object>() { owner, sId, pId });
                StoredProcedures.InsertNews(owner, DateTime.Now, "You have colonized " + Systems.SystemName(sId) + " " + pId.ToString() + "(" + sId + " #" + pId + ")");
            }
            catch (Exception ex)
            {
                throw new Exception(sId.ToString() + " _ " + pId.ToString() + " _ " + owner.ToString(), ex);
            }
        }
        public static void RandomGet(int sId, int pId, int owner)
        {
            //TODO: add artifacts here
            Random r = new Random();
            int t = r.Next(1000);
            int bonus = 0;
            if (t < 100)
                bonus = r.Next(10, 150);
            else if (t < 200)
                bonus = r.Next(80, 200);
            else if (t < 300)
                bonus = r.Next(100, 500);
            else if (t < 400)
                bonus = r.Next(250, 500);
            else if (t < 500)
                bonus = r.Next(300, 600);
            else if (t < 900)
                bonus = r.Next(500, 1000);
            else if (t < 950)
                bonus = r.Next(800, 1000);
            else if (t < 990)
                bonus = r.Next(900, 1500);
            else if (t < 997)
                bonus = r.Next(1000, 2000);
            else bonus = r.Next(2000, 6000);
            CashFunctions.AddCash(owner, bonus);
            StoredProcedures.InsertNews(pId, DateTime.Now, "You have found on the " + Systems.SystemName(sId) + " #" + pId.ToString() + " $" + bonus.ToString() + ".");
        }

        public static void ConquerPlanet(int sId, int pId, int owner)
        {
            int oldPlanetOwner = Planets.PlanetOwner(sId, pId);
            if (oldPlanetOwner  == 1)
            {
                RandomGet(sId, pId, owner);

            }
            DAL.GetInstance().UpdateData("Update Planets SET OwnerId= @param0, PopulationLevel =1, PopToNextLevel = 21 Where SystemID = @param1 and PlanetID = @param2", new List<object>() { owner, sId, pId });
            StoredProcedures.InsertNews(owner, DateTime.Now, "You have conquered " + Systems.SystemName(sId) + " " + pId.ToString() + "(" + sId + " #" + pId + ")");
            StoredProcedures.InsertNews(oldPlanetOwner, DateTime.Now, "You have lost " + Systems.SystemName(sId) + " " + pId.ToString() + "(" + sId + " #" + pId + ") conquered by: " + UserFunctions.PlayerName(owner));
        }
        public static void KillPopOnPlanet(int sId, int pId, int owner, ref int tr)
        {
            int pop = Planets.PopulationOnPlanet(sId, pId);
            int newPop;
            if (tr < pop)
            {
                newPop = pop - tr;
                tr = 0;
            }
            else
            {
                newPop = 1;
                tr -= (pop - newPop);
            }
            DAL.GetInstance().UpdateData("Update Planets SET PopulationLevel = @param1, PopToNextLevel = @param2 Where SystemID = @param3 and PlanetID = @param4", new List<object>() { owner, newPop, Equations.PopulationLevel(newPop + 1), sId, pId });
            if (pop - newPop != 0)
            {
                StoredProcedures.InsertNews(owner, DateTime.Now, "You have killed " + (pop - newPop).ToString() + " population on planet " + Systems.SystemName(sId) + " " + pId.ToString() + "(" + sId + " #" + pId + ")");
                StoredProcedures.InsertNews(Planets.PlanetOwner(sId, pId), DateTime.Now, "You have lost " + (pop - newPop).ToString() + " population on planet " + Systems.SystemName(sId) + " " + pId.ToString() + "(" + sId + " #" + pId + "). Killed by " + UserFunctions.PlayerName(owner));
            }
        }
    };
}